﻿/**
 * Created by chesw on 2017/5/11.
 * describe：病例详情
 */
(function () {
    new Vue({
        el: '#case_detail',
        data: {
            caseReportList: [],
            eReportDetail: '',
            caseInfoList: [],
            pageInfo: {
                allRecord: [],
                curPageRecord: [],
                totalCount: "",
                pageSize: 10,
                pageCount: 10,
                pageCurrent: 1
            },
            fileInfo: {
                fileName: "",
                htmlContent:""
            },
            showPageParam: {}
        },
        components: {
            'clinic-page': clinicPage
        },
        created: function () {
            var _self = this;
            var p_caseId = getQueryString("caseId");
            var p_orgId = getQueryString("orgId");
            var p_visitId = getQueryString("visitID");
            
            if (p_caseId != null && p_orgId != null && p_visitId != null) {
                _self.GetEReportDetail(p_caseId, p_orgId);
                _self.GetCaseReportList(p_visitId);
            }
            else
            {
                _self.ShowPageBox(false);
            }
        },     
        methods: {
            ShowPageBox: function (p_show, p_pageObject) {
                var resultEmpty = document.getElementById("result-empty");
                var pageBox = document.getElementById("clinic-page-box");
                if (p_show) {
                    if (resultEmpty!=null)
                        resultEmpty.style.display = "none";

                    if(pageBox!=null){
                        if (p_pageObject.pageCount <= 1)
                            pageBox.style.display = "none";
                        else
                            pageBox.style.display = "block";
                    }                  
                }
                else {
                    if(resultEmpty!=null)
                        resultEmpty.style.display = "block";
                    if(pageBox!=null)
                        pageBox.style.display = "none";             
                }
            },
            GetEReportDetail: function (p_caseId, p_orgId) {
                var _self = this;
                _self.$http.post("/PersonalCenter/GetEReportDetail", { caseId: p_caseId, OrgId: p_orgId }).then(function (res) {
                    var caseInfoContent = document.getElementById("case-info");
                    caseInfoContent.innerHTML = res.body;
                });
            },
            GetCaseReportList: function (p_visitId) {
                var _self = this;
                _self.$http.post("/PersonalCenter/GetCaseReportList", { visitId: p_visitId}).then(function (res) {
                    if (res.body.length != 0) {
                        for (var i = 0; i < res.body.length; i++) {
                            _self.pageInfo.allRecord.push(res.body[i]);
                        }
                        _self.pageInfo.totalCount = _self.pageInfo.allRecord.length;
                        _self.pageInfo = showPageRecords(1, _self.pageInfo);
                        _self.showPage();
                        _self.ShowPageBox(true, _self.pageInfo);
                    }
                    else {
                        _self.ShowPageBox(false);
                    }
                });
            },
            showPage: function () {
                var _self = this;
                var pageBox = document.getElementById("clinic-page-box");
            
                if (_self.pageInfo.pageCount === 1)
                    pageBox.style.display = "none";
                else
                    pageBox.style.display = "block";

                var param = {
                    pageCount: _self.pageInfo.pageCount,
                    curPage: _self.pageInfo.pageCurrent,
                    pageInfo: _self.pageInfo,
                    page_func: function (p_curPage, p_pageInfo) {
                        showPageRecords(p_curPage, p_pageInfo);
                    }
                };
                _self.showPageParam = param;
               
                _self.$emit('show', param);
            },
            view: function (p_index) {
                var _self = this;
                _self.$http.post("/PersonalCenter/ViewDetail", _self.pageInfo.allRecord[p_index]).then(function (res) {
                    var caseReport = document.getElementById("case-report");
                    var reportDetailContent = document.getElementById("report-detail");
                    if (res.body.Code !== -1) {
                        caseReport.style.display = "block";
                        reportDetailContent.innerHTML = res.body;
                        _self.$nextTick(function () {
                            $(".box-content").css("position", "relative");
                        });
                    }                  
                });
            },
            closeDetail: function () {
                var caseReport = document.getElementById("case-report");
                caseReport.style.display = "none";
            },
            DownLoadReportDetail: function () {
                var _self = this;
                var reportDetailContent = document.getElementById("report-detail");
                _self.fileInfo.htmlContent = reportDetailContent.innerHTML;
                _self.$http.post("/PersonalCenter/DownloadReportDetail", _self.fileInfo).then(function (res) {               
                    var fileLoader = window.open(res.body, "_blank");
                    fileLoader.document.title = "文件下载";
                });
            },
            downloadCase: function () {
                var _self = this;
                var caseDetailContent = document.getElementById("case-info");
                _self.fileInfo.htmlContent = caseDetailContent.innerHTML;
                if (!isEmpty(_self.fileInfo.htmlContent)) {
                    _self.$http.post("/PersonalCenter/DownloadCase", _self.fileInfo).then(function (res) {
                        var fileLoader = window.open(res.body, "_blank");
                        fileLoader.document.title = "文件下载";
                    });
                }              
            }
        }
    })
})();